 
  

 






<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=157 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
<head>
 <title>
  Java Practices -> Using JavaHelp
 </title>
 <link rel="stylesheet" type="text/css" href="../stylesheet8.css" media="all">
 
 <link rel="shortcut icon" href='../images/favicon.ico' type="image/vnd.microsoft.icon">
 <meta name="description" content="Concise presentations of java programming practices, tasks, and conventions, amply illustrated with syntax highlighted code examples.">
 
 <meta name='keywords' content='HelpAction,JavaHelp,swing,java,java programming,java practices,java idiom,java style,java design patterns,java coding conventions,'>
 
 
</head>
 
<body>


<div class='menu-bar'>
 
  <a href='../home/HomeAction.html' title='Table of Contents'>Home</a> |
  <a href='../vote/VoteSummaryAction-2.html' title='View Poll Results'>Poll</a> |
   
  <A href='../feedback/FeedbackAction451f-2.html?Operation=Show' title='Send Your Feedback'>Wiki</a> |
  <b><a href='../source/SourceAction-2.html' title='Grab Source Code'>Source Code</a></b><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |

  <a href='http://www.web4j.com/Java_Web_Application_Framework_Overview.jsp?From=1' title='Free Download - Java Web Application Framework'><b>WEB4J</b></a> |
  
  <a href='http://www.date4j.net/' title='Replacement for java.util.Date'><b>DATE4J</b></a> |

   <a href='../references/ReferencesAction-2.html' title='References'>Links</a>
   
  <form action='http://www.javapractices.com/search/SearchAction.do' method='get' class='search-form'>
   <input type='text' name='SearchTerms' value="" size=12 maxlength=50 class='search'>
   <input type='submit' value="Search">
  </form>
 
</div>

<P>



  

 






<p class="display-messages">

 

 

</p>


<div class="main-layout">
 
   

 




<div class='page-title'>Using JavaHelp</div>

<div class='main-body'>
 
<br>Sun's <a href="http://java.sun.com/products/javahelp/">JavaHelp</a>
tool creates help systems for graphical applications, using HTML topic
files and XML to define the topic hierarchy. Developers access its services
through the <tt>javax.help</tt> package.
<p>Here is an example of using <tt>javax.help</tt> to launch a JavaHelp
system from a standard Help menu.
<br>
<PRE>

<span class='keyword'>package</span> hirondelle.stocks.help;

<span class='keyword'>import</span> java.awt.event.*;
<span class='keyword'>import</span> javax.swing.*;
<span class='keyword'>import</span> java.util.logging.*;
<span class='keyword'>import</span> javax.help.*;
<span class='keyword'>import</span> java.net.URL;
<span class='keyword'>import</span> hirondelle.stocks.util.Args;
<span class='keyword'>import</span> hirondelle.stocks.util.Util;

<span class='comment'>/**
* Display the help system for the application.
*
* &lt;P&gt;Display one of table of contents, index, or search tab, according 
* to argument passed to the constructor. This implementation uses 
* Sun's &lt;a href=http://java.sun.com/products/javahelp/&gt;JavaHelp&lt;/a&gt; tool.
*
* &lt;P&gt;This action activates the Help key (often &lt;tt&gt;F1&lt;/tt&gt;) for this application. 
* When the help key is pressed, the help system's table of contents is displayed.
*
* &lt;P&gt;This action is unusual in that it corresponds to more than one menu item 
* (Contents, Index, and Search).
*
* &lt;P&gt;Note: the displayed JavaHelp screen is not centered; it is left as is, 
* since the JavaHelp GUI is often cut off at the bottom anyway, and centering would 
* make this problem worse.
*/</span>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> HelpAction <span class='keyword'>extends</span> AbstractAction {

  <span class='comment'>/**
  * Constructor.
  *  
  * @param aFrame parent window to which the help window is attached
  * @param aText name of the menu item for this help action
  * @param aMnemonicKeyEvent mnemonic for &lt;tt&gt;aText&lt;/tt&gt;
  * @param aIcon possibly-null graphic to be displayed alongside the text, or 
  * in a toolbar
  * @param aView determines which help window is to be displayed: Contents, Index, 
  * or Search
  */</span>
  <span class='keyword'>public</span> HelpAction(
    JFrame aFrame, String aText, <span class='keyword'>int</span> aMnemonicKeyEvent, Icon aIcon, View aView
  ) {
    <span class='keyword'>super</span>(aText, aIcon);
    Args.checkForNull(aFrame);
    Args.checkForNull(aText);
    Args.checkForNull(aView);
    fFrame = aFrame;
    fView = aView;
    putValue(SHORT_DESCRIPTION, <span class='literal'>"StocksMonitor Help"</span>);
    putValue(LONG_DESCRIPTION, <span class='literal'>"Displays JavaHelp for StocksMonitor."</span>);
    putValue(MNEMONIC_KEY, <span class='keyword'>new</span> Integer(aMnemonicKeyEvent) );    
    initHelpSystem(); 
  }

  <span class='keyword'>public</span> <span class='keyword'>void</span> actionPerformed(ActionEvent event) {
    fLogger.info(<span class='literal'>"Showing help system."</span>);
    fHelpBroker.setCurrentView( fView.toString() );
    fDisplayHelp.actionPerformed( event );
  }
  
  <span class='comment'>/** Enumeration for the style of presentation of the the Help system. */</span>
  <span class='keyword'>public</span> <span class='keyword'>enum</span> View {
    SEARCH(<span class='literal'>"Search"</span>), 
    CONTENTS(<span class='literal'>"TOC"</span>), 
    INDEX(<span class='literal'>"Index"</span>);
    <span class='keyword'>@Override</span> <span class='keyword'>public</span> String toString(){
      <span class='keyword'>return</span> fName;
    }
    <span class='keyword'>private</span> View(String aName){
      fName = aName;
    }
    <span class='keyword'>private</span> String fName;
  } 

  <span class='comment'>// PRIVATE //
</span>  <span class='keyword'>private</span> JFrame fFrame;
  <span class='keyword'>private</span> View fView;
  <span class='comment'>/** Path used by a classloader to find the JavaHelp files. */</span>
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> String PATH_TO_JAVA_HELP = 
    <span class='literal'>"hirondelle/stocks/help/JavaHelp/HelpSet.hs"</span>
  ;
  <span class='keyword'>private</span> ClassLoader DEFAULT_CLASS_LOADER = <span class='keyword'>null</span>;
  <span class='keyword'>private</span> <span class='keyword'>static</span> <span class='keyword'>final</span> Logger fLogger = Util.getLogger(HelpAction.<span class='keyword'>class</span>); 
  
  <span class='keyword'>private</span> HelpBroker fHelpBroker;
  <span class='keyword'>private</span> CSH.DisplayHelpFromSource fDisplayHelp;
  
  <span class='comment'>/** Initialize the JavaHelp system. */</span>
  <span class='keyword'>private</span> <span class='keyword'>void</span> initHelpSystem(){
    <span class='comment'>//optimization to avoid repeated init
</span>    <span class='keyword'>if</span> ( fHelpBroker != <span class='keyword'>null</span> &amp;&amp; fDisplayHelp != <span class='keyword'>null</span>) <span class='keyword'>return</span>;
    
    <span class='comment'>//(uses the classloader mechanism)
</span>    ClassLoader loader = <span class='keyword'>this</span>.getClass().getClassLoader();
    URL helpSetURL = HelpSet.findHelpSet(loader, PATH_TO_JAVA_HELP);
    assert helpSetURL != <span class='keyword'>null</span> : <span class='literal'>"Cannot find help system."</span>;
    <span class='keyword'>try</span> {
      HelpSet helpSet = <span class='keyword'>new</span> HelpSet(DEFAULT_CLASS_LOADER, helpSetURL);
      fHelpBroker = helpSet.createHelpBroker();
      fHelpBroker.enableHelpKey( fFrame.getRootPane(), <span class='literal'>"overview"</span>, helpSet );
      fDisplayHelp = <span class='keyword'>new</span> CSH.DisplayHelpFromSource(fHelpBroker);
    }
    <span class='keyword'>catch</span> (HelpSetException ex) {
      fLogger.severe(<span class='literal'>"Cannot create help system with: "</span> + helpSetURL);
    }
    assert fHelpBroker != <span class='keyword'>null</span> : <span class='literal'>"HelpBroker is null."</span>;
  }
}
 
</PRE>
<br>
<br>
<br>

</div>





<div class='topic-section'>Would you use this technique?</div>
<div class='main-body'>
  
  <form action="http://www.javapractices.com/vote/AddVoteAction.do" method='post'>
    Yes<input type='radio' name='Choice' value='Y' >
    &nbsp;&nbsp;No<input type='radio' name='Choice' value='N'>
    &nbsp;&nbsp;Undecided<input type='radio' name='Choice' value="?" >
    &nbsp;&nbsp;<input type=submit value="Vote" >
    <input type='hidden' name='Operation' value='Apply'>
    <input type='hidden' name='TopicId' value='157'>
  </form>
</div>

<div style='height:10.0em;'></div>

 
 
</div>

  

 





<div align='center' class='legalese'>  
&copy; 2011 Hirondelle Systems |
<a href='../source/SourceAction-2.html'><b>Source Code</b></a><IMG class='no-margin' SRC="../images/goldstar.gif" ALT=""> |
<a href="mailto:webmaster@javapractices.com">Contact</a> |
<a href="http://creativecommons.org/licenses/by-nc-sa/1.0/">License</a> |
<a href='../apps/cjp.rss'>RSS</a>
<!-- ukey="2AC36CD2" -->
<!-- ckey="16DF3D87" -->
<br>

 Individual code snippets can be used under this <a href='../LICENSE.txt'>BSD license</a> - Last updated on June 6, 2010.<br>
 Over 150,000 unique IPs last month - <span title='Java Practices 2.6.5, Mon May 16 00:00:00 EDT 2011'>Built with</span> <a href='http://www.web4j.com/'>WEB4J</a>.<br>
 - In Memoriam : Bill Dirani -
</div>

<script src="../../www.google-analytics.com/urchin.js" type="text/javascript">
</script>
<script type="text/javascript">
_uacct = "UA-2633428-1";
urchinTracker();
</script>



</body>

<!-- Mirrored from www.javapractices.com/topic/TopicAction.do?Id=157 by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:28:07 GMT -->
<!-- Added by HTTrack --><meta http-equiv="content-type" content="text/html;charset=UTF-8"><!-- /Added by HTTrack -->
</html>
